<?php

/**
 * @author juanma
 * @copyright 2012
 */


class Sgit_Graficos_ProyectoImputacion extends Sgit_Graficos_Parent
{


    private function getData()
    {

        $m_imputacion = $this->loadModel('InformeElectricidadMensualProyecto');
        $m_proyecto = $this->loadModel('Proyectos');


        $this->key = $this->request->getParam('key', 'potencia');
        $this->id = $this->request->getParam('id');


        
        switch($this->key){
            case'consumo_it':
                $this->y = 'kWh';
                $this->yaxistitle='kWh';
                $this->subtitle="Consumo kWh";
                $this->format = '%d';
                break;
            case'potencia':
                $this->y = 'W';
                $this->yaxistitle='Watios';
                $this->subtitle="Potencia Watios";
                $this->format='%d';
                break;
            case'imputacion':
                $this->y = '€';
                $this->yaxistitle='Euros';
                $this->subtitle="Imputación Euros";
                $this->format = '%d';
                break;
            case'facturacion':
                $this->y = '€';
                $this->yaxistitle='Euros';
                $this->subtitle="Facturación Euros";
                $this->format = '%d';
                break;
            case'consumo_facturable':
                $this->y = 'kWh';
                $this->yaxistitle='kWh';
                $this->subtitle="Consumo facturable";
                $this->format = '%d';
                break;
            case'consumo_imputable':
                $this->y = 'kWh';
                $this->yaxistitle='kWh';
                $this->subtitle="Consumo Imputable";
                $this->format = '%d';
                break;
            case'imputacion_exceso':
                $this->y = '€';
                $this->yaxistitle='';
                $this->subtitle="Imputación Exceso";
                $this->format = '%d';
                break;
            case'imputacion_disponibilidad_contratada':
                $this->y = '€';
                $this->yaxistitle='';
                $this->subtitle="Imputación Disponibilidad Contratada";
                $this->format = '%d';
                break;   
            case'imputacion_disponibilidad_consumida':
                $this->y = '€';
                $this->yaxistitle='';
                $this->subtitle="Imputación Disponibilidad Consumida";
                $this->format = '%d';
                break;
            case'disponibilidad':
                $this->y = '%';
                $this->yaxistitle='%';
                $this->subtitle="% de Utilización";
                $this->format = '%d';
                break;
            case'ratio_consumido_contratado':
                $this->y = '%';
                $this->yaxistitle='%';
                $this->subtitle="% de Utilización";
                $this->format = '%d';
                break;
            case'disponibilidad_wm2_consumida':
                $this->y = 'W/m2';
                $this->yaxistitle='W/m2';
                $this->subtitle="Ratio W/m2 Consumido";
                $this->format = '%d';
                break;    
            default:
                $this->y='';
                $this->yaxistitle='';
                $this->subtitle=$this->key;
                $this->format = '%0.02f';
            case'margen':
                $this->y = '%';
                $this->yaxistitle='%';
                $this->subtitle="Margen Venta / Coste";
                $this->format = '%d';
                break;  
        }




        $proyecto=$m_proyecto->GetRow($this->id);



        $filas = $m_imputacion->getAll(array('id_proyecto=?' => $this->id), array('anio DESC', 'mes DESC'), null, $this->limit);
        $filas = array_reverse($filas);



        $this->title = 'Proyecto '.Sgit_Cache::single()->model('Datacenter')->getKey($filas[0]['id_zona']).': '. $proyecto['nombre'];
        $this->subtitle = $this->subtitle;



        $i = 0;
        $fecha = new Zend_Date();
		$fecha->setDay(1);
        foreach ($filas as $v)
        {

            $fecha->setMonth($v['mes'])->setYear($v['anio']);

            $this->label[$i]=ucfirst($fecha->get('MMM-yy'));
            $this->labelJsonFormat[$i]=ucfirst($fecha->get('MMM-yyyy'));
            $this->labelJson[$i]=$fecha->get('yyyy-MM-dd');
            
            $fecha->subMonth(1);
            $v['potencia'] =1000*Sgit_Util_Potencia::run($v['consumo_it'], $fecha);
            //$v['margen']=(100*$v['facturacion']/$v['imputacion'])-100;
            
            $v['margen']=(($v['facturacion']/$v['imputacion'])-1)*100;
            
            
            $this->value['imputacion'][$i] =$this->setValue($v['imputacion']);
            $this->value['facturacion'][$i] =$this->setValue($v['facturacion']);

            

            if(isset($v[$this->key]))
            {
                $this->value[$this->key][$i] =$this->setValue($v[$this->key]);
            }





           
            if($v['tipo_disponibilidad']==2)
            {
                $this->value['disponibilidad_wm2_contratada'][$i] =$this->setValue($v['disponibilidad_wm2_contratada']*3);
                $this->value['disponibilidad_wm2_consumida'][$i] =$this->setValue($v['disponibilidad_wm2_consumida']*3);
           
                if($this->key=='disponibilidad_wm2_consumida'){
                    $this->y = 'W/rack';
                    $this->yaxistitle='W/rack';
                    $this->subtitle="Ratio W/rack Consumido";
                }
            }else{
                $this->value['disponibilidad_wm2_contratada'][$i] =$this->setValue($v['disponibilidad_wm2_contratada']);

            }





            
            $i++;
        }
        
        

    }


    public function jpGrapgh()
    {

        $this->getData();
        


        $this->loadJpgraph('bar');
        $this->loadJpgraph('line');


        $step = $this->_step(count($this->value[$this->key]), $this->width);
        $nombre = md5($this->key . $this->width . $this->height . serialize($this->value[$this->key]));


        $graph = $this->_graph($this->width, $this->height);
        $graph->SetMargin(75, 75, 30, 80);

        $graph->title->Set(utf8_decode($this->title));
        $graph->subtitle->Set(utf8_decode($this->subtitle));
        $graph->yaxis->SetTitle($this->yaxistitle, 'middle');
        $graph->xaxis->SetTickLabels($this->label);
        $graph->xaxis->SetTextLabelInterval($step);
        $graph->xaxis->SetLabelAngle(90);


        $barplot = $this->_barPlot($this->value[$this->key], '#c0c0c0');
        $barplot->value->Show();
        $barplot->value->SetFont(FF_ARIAL, FS_BOLD, 8);
        $barplot->value->SetAngle(90);
        $barplot->value->SetFormat($this->format);
        $barplot->SetValuePos('center');
        $barplot->SetFillGradient( $this->color . '@0.3',  $this->color . '@0.6', GRAD_VERT);
        $barplot->SetWidth(1);
        $barplot->SetValuePos('center');
        $graph->Add($barplot);

        if($this->key=='disponibilidad_wm2_consumida')
        {
            $lineplot2= $this->_linePlot($this->value['disponibilidad_wm2_contratada'],"$this->y Contratado",'red',3,false);
            $lineplot2->SetBarCenter();
            $graph->Add($lineplot2);
        }
        
        if($this->key=='imputacion')
        {
            $lineplot2= $this->_linePlot($this->value['facturacion'],'Facturación €','red',2,false);
            $lineplot2->SetBarCenter();
            $graph->Add($lineplot2);
        }
        
        if($this->key=='facturacion')
        {
            $lineplot2= $this->_linePlot($this->value['imputacion'],'Imputación €','red',2,false);
            $lineplot2->SetBarCenter();
            $graph->Add($lineplot2);
        }
        
        if($this->key=='ratio_consumido_contratado')
        {
            $line = new PlotLine(HORIZONTAL,100,"red",2);
            $graph->AddLine($line);
        }
        

        $graph->footer->left->Set(utf8_decode("Fuente: Lecturas mensuales de contadores del cliente"));
        $graph->Stroke();


    }

    public function googleVisualization(&$view)
    {

        $this->limit = null;
        $this->getData();

        $view->title = $this->title . ' ' . $this->subtitle;
        $view->subtitle = $this->subtitle;
        $view->options = "
                'title': '{$view->title}',
                'hAxis': {'slantedText': true,slantedTextAngle:90},
                'legend': {'position': 'right'},
                'animation' : {'duration' : 2000},
                'curveType' : 'function',
                'vAxis' : {'title': 'pue'},
                 strictFirstColumnType: false,
                'vAxis' : {'minValue': 0, title: '$this->yaxistitle'},
                 colors:['green'],
        ";
        $view->y = $this->y;
        $view->label = $this->labelJson;
        $view->label_format = $this->labelJsonFormat;
        $view->data = $this->value[$this->key];


    }


}
